package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/regions")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private ServletContext servletContext;
    @Autowired
    private IStrategyService strategyService;

    /**
     * 获取所有推荐的大地区
     *
     * @return
     */
    @GetMapping
    public List<Region> listAll(Integer state) {
        List<Region> regions = new ArrayList<>();
        if (state == null) {
            regions = regionService.listAll();
        } else {
            regions = regionService.listByState(state);
        }
        return regions;
    }

    /**
     * 获取某个地区下的攻略数据
     *
     * @return
     */
    @GetMapping(value = {"/{regionId}/strategies"}, produces = {"text/html;charset=utf-8"})
    public void queryStrategise(StrategyQueryObject qo, HttpServletResponse response) throws Exception {
        //查询某一个地区下的热门攻略
        qo.setState(Strategy.STATE_HOT);//热门攻略
        qo.setPageSize(0);//不分页
        PageInfo hot = strategyService.query(qo);

        //查询某个地区下所有的非禁用攻略攻略
        qo.setState(null);//热门攻略
        qo.setNotForbidden(true);//查询非禁用状态的攻略
        qo.setPageSize(4);//不分页
        PageInfo all = strategyService.query(qo);
        Map<String, Object> root = new HashMap<>();
        root.put("hot", hot);
        root.put("all", all);

        //1.创建freemarker的配置对象
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        //2.获取template目录的真实路径
        String realPath = servletContext.getRealPath("templates");
        System.out.println(realPath);
        //设置模板的目录
        configuration.setDirectoryForTemplateLoading(new File(realPath));
        //3.设置模板编码
        configuration.setDefaultEncoding("UTF-8");
        //4.获取模板文件对象
        Template template = configuration.getTemplate("test.ftl");
        //5.设置响应编码
        response.setContentType("text/html:chatset=utf-8");
        //6.合并数据
        template.process(root, response.getWriter());
    }

    @GetMapping(value = {"/{regionId}/strategies"}, produces = {"application/json;charset=utf-8"})
    public PageInfo queryStrategiesByRegionId(StrategyQueryObject qo) {
        qo.setPageSize(4);
        return this.strategyService.query(qo);
    }
}

